using System.Collections.Generic;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;

namespace Game.Component
{
	public class EyeMachineItem : BaseItem
	{
		private EyeMachineDisplayerItem displayer;

		private BaseItem targetCharacter;

		private List<BaseItem> eyeTestTools = new List<BaseItem>();

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		public void SetTargetCharacter(BaseItem item)
		{
			targetCharacter = item;
			CheckDisplayer();
		}

		public void AddEyeTestTool(BaseItem item)
		{
			if (!eyeTestTools.Contains(item))
			{
				eyeTestTools.Add(item);
			}
			CheckDisplayer();
		}

		public void RemoveEyeTestTool(BaseItem item)
		{
			eyeTestTools.Remove(item);
			CheckDisplayer();
		}

		public void CheckDisplayer()
		{
			if (displayer == null)
			{
				BaseItem itemByScriptType = Singleton<Scene>.Instance.GetItemByScriptType(108);
				if (itemByScriptType != null)
				{
					displayer = (EyeMachineDisplayerItem)itemByScriptType;
				}
			}
			if (!(displayer != null))
			{
				return;
			}
			if (targetCharacter == null)
			{
				displayer.SetDisplayerActive(false);
				return;
			}
			if (eyeTestTools.Count > 0 && !string.IsNullOrEmpty(eyeTestTools[eyeTestTools.Count - 1].itemData.scriptParams))
			{
				string[] array = eyeTestTools[eyeTestTools.Count - 1].itemData.scriptParams.Split(',');
				if (array.Length == 3)
				{
					displayer.SetImageId(int.Parse(array[0]));
				}
				else
				{
					displayer.SetImageId(0);
				}
			}
			else
			{
				displayer.SetImageId(0);
			}
			displayer.SetDisplayerActive(true);
		}
	}
}
